---@diagnostic disable: undefined-global

--- 惠盟技能
--- 弃牌阶段开始前，你可以视为使用一张【以逸待劳】。若中央区的花色数没有因此增加，结束此阶段，否则令因此使中央区新增花色的角色摸一张牌。
local huimeng = fk.CreateSkill{
  name = "lvshi__huimeng",
  frequency = "skill",
}

local ls = require "packages/lvshi/utility"
local DIY = require "packages/diy_utility/diy_utility"

huimeng:addAcquireEffect(function (self, player, is_start)
  player.room:addSkill("#CenterArea")
end)

huimeng:addEffect(fk.EventPhaseChanging, {
  anim_type = "control",
  can_trigger = function(self, event, target, player, data)
    return target == player and player:hasSkill(huimeng.name) and
    data.phase == Player.Discard and not data.skipped
  end,
  on_cost = function(self, event, target, player, data)
    return player.room:askToSkillInvoke(player, { skill_name = huimeng.name, prompt = "#huimeng-invoke" })
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    -- 记录原始中央区花色数
    local original_suit_count = ls.countSuits(DIY.getCenterCards(room))
    local original_suits = {}
    local center_cards = DIY.getCenterCards(room)
    for _, card_id in ipairs(center_cards) do
      local card = Fk:getCardById(card_id)
      table.insertIfNeed(original_suits, card.suit)
    end
    -- 视为使用以逸待劳
    local targets = table.filter(room:getAlivePlayers(), function(p) return ls.isFriend(player, p) end)
    local use = room:useVirtualCard("lvshi__await_exhausted", nil, player, targets, huimeng.name)
    use.extra_data = use.extra_data or {}
    use.extra_data.discards = use.extra_data.discards or {}
    -- 检查中央区花色数是否增加
    local new_suit_count = ls.countSuits(DIY.getCenterCards(room))
    if new_suit_count <= original_suit_count then
      -- 花色数没有增加，结束弃牌阶段
      data.skipped = true
      room:sendLog{
        type = "#ls__huimeng_skip",
        from = player.id,
        arg = huimeng.name,
      }
    else
      -- 花色数增加，令相关角色摸一张牌
      local players = {}
      local logic = room.logic
      -- 获取使用以逸待劳后的弃牌事件
      logic:getEventsByRule(GameEvent.MoveCards, 1, function(e)
        for _, move in ipairs(e.data) do
          -- 检查是否是从手牌或装备区弃置到弃牌堆
          if move.from and move.toArea == Card.DiscardPile then
            -- 检查弃置的卡牌花色是否为新花色
            for _, info in ipairs(move.moveInfo) do
              if table.contains({ Card.PlayerHand, Card.PlayerEquip }, info.fromArea) then
                local card = Fk:getCardById(info.cardId)
                if card.suit ~= Card.NoSuit and not table.contains(original_suits, card.suit) then
                  table.insertIfNeed(players, move.from)
                  break
                end
              end
            end
          end
        end
      end, 0)
      if #players > 0 then
        for _, p in ipairs(players) do
          p:drawCards(1, huimeng.name)
        end
      end
    end
  end,
})

Fk:loadTranslationTable{
  ["lvshi__huimeng"] = "惠盟",
  [":lvshi__huimeng"] = "弃牌阶段开始前，你可以视为使用一张【以逸待劳】。若中央区的花色数没有因此增加，结束此阶段，否则令因此使中央区新增花色的角色摸一张牌。",
  ["$lvshi__huimeng1"] = "以静制动，后发制人。",
  ["$lvshi__huimeng2"] = "养精蓄锐，蓄势待发。",
  
  ["#huimeng-invoke"] = "惠盟：是否视为使用一张【以逸待劳】？",
  ["#ls__huimeng_skip"] = "%from 发动了【%arg】，弃牌阶段被跳过",
}

return huimeng